package com.wang.rabbitmq

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import org.springframework.amqp.core.Message
import org.springframework.amqp.rabbit.connection.CorrelationData
import java.util.*
import kotlin.collections.ArrayList
import kotlin.system.exitProcess

fun main() {
    val rabbitTemplate = RabbitMq.buildMessageSender(
        "192.168.1.147",
        "5674",
        "admin",
        "advance.ai2016",
        "genie_channel_sync_direct_exchange",
        "pull_chat_message_output_queue"
    )
    for (s in buildData()) {
        println(s)
        rabbitTemplate.send(Message(s.toByteArray()))
    }

//    val scan = Scanner(System.`in`)
//    while (true) {
//        val s = scan.nextLine()
//        if ("exit".equals(s)) {
//            rabbitTemplate.destroy()
//            break;
//        }
//        rabbitTemplate.correlationConvertAndSend(s, CorrelationData(System.currentTimeMillis().toString()))
//    }
    rabbitTemplate.destroy()
    exitProcess(0)
    println("end")
}

fun buildData(): List<String> {
    var s = """{
    "code": "SUCCESS",
    "message": "OK",
    "data": {
        "channel": "SHOPEE_ID",
        "genieShopId": null,
        "genieMerchantId": null,
        "genieTransactionId": "6df7204ee88aa999",
        "channelShopId": "219866242",
        "errorInfo": null,
        "errorDetail": null,
        "channelSessionId": "928251542234657885",
        "messageList": [
            {
                "messageId": "2133142337377304951",
                "senderId": "219870301",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "order",
                "genieChatMessageType": "PICTURE",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": null,
                    "emojiId": null,
                    "emojiPackageId": null,
                    "url": "https://ecs7.tokopedia.net/img/chat/sticker/buyer_8.png",
                    "productId": null,
                    "orderSn": "220207TR58ET2U"
                }
            },
            {
                "messageId": "2133141867665572209",
                "senderId": "219870301",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "order",
                "genieChatMessageType": "ORDER",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": null,
                    "emojiId": null,
                    "emojiPackageId": null,
                    "imageUrl": null,
                    "productId": null,
                    "orderSn": "220207TR58ET2U"
                }
            },
            {
                "messageId": "2133141314025832819",
                "senderId": "219870301",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "item",
                "genieChatMessageType": "PRODUCT",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": null,
                    "emojiId": null,
                    "emojiPackageId": null,
                    "imageUrl": null,
                    "productId": "11216333827",
                    "orderSn": null
                }
            },
            {
                "messageId": "2133141133869973875",
                "senderId": "219870301",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "item",
                "genieChatMessageType": "PRODUCT",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": null,
                    "emojiId": null,
                    "emojiPackageId": null,
                    "imageUrl": null,
                    "productId": "11216333827",
                    "orderSn": null
                }
            },
            {
                "messageId": "2133140082966118769",
                "senderId": "219870301",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "text",
                "genieChatMessageType": "NORMAL_TEXT",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": "asjdajksd",
                    "emojiId": null,
                    "emojiPackageId": null,
                    "imageUrl": null,
                    "productId": null,
                    "orderSn": null
                }
            },
            {
                "messageId": "2133140020993163634",
                "senderId": "219870301",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "text",
                "genieChatMessageType": "NORMAL_TEXT",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": "asjdajksd",
                    "emojiId": null,
                    "emojiPackageId": null,
                    "imageUrl": null,
                    "productId": null,
                    "orderSn": null
                }
            },
            {
                "messageId": "1687236336281583741",
                "senderId": "7920546",
                "senderName": null,
                "conversationId": null,
                "externalReadStatus": null,
                "externalSendStatus": null,
                "genieChatMessageSendStatus": "SUCCESS",
                "externalMessageType": "text",
                "genieChatMessageType": "NORMAL_TEXT",
                "externalSenderType": null,
                "genieChatSenderType": null,
                "product": null,
                "image": null,
                "messageContent": {
                    "text": "masihada?",
                    "emojiId": null,
                    "emojiPackageId": null,
                    "imageUrl": null,
                    "productId": null,
                    "orderSn": null
                }
            }
        ]
    },
    "extra": null,
    "pricingStrategy": "PAY"
}"""
    val json = JSONObject.parseObject(s)
    for (msg in ((json["data"] as JSONObject)["messageList"] as JSONArray)) {
        msg as JSONObject
        msg["messageId"] = UUID.randomUUID().toString()
        msg["genieChatSenderType"] = "BUYER"
    }
    ((json["data"] as JSONObject)["messageList"] as JSONArray).removeIf { "PICTURE" != (it as JSONObject)["genieChatMessageType"] }
    return arrayListOf(json.toString())
}

fun toko(): List<String> {
    val s = """{"code":"SUCCESS","message":"OK","data":{"channel":"TOKOPEDIA_ID","genieShopId":"SH607673E9DC0E820001825143","genieMerchantId":"MC6001B465E82886000174110D","genieTransactionId":"1918c9a8193700d2","channelShopId":"10982627","channelSessionId":"1735711991","messageList":[{"senderId":"185924262","sendTime":"2022-02-14T06:32:06.365Z","genieChatMessageSendStatus":"SUCCESS","genieChatMessageType":"PICTURE","genieChatSenderType":"BUYER","messageContent":{"url":"https://images.tokopedia.net/img/LUZQDL/2022/2/14/79b19abc-6954-4534-8d80-75058a945310.jpg"}}]},"pricingStrategy":"PAY"}"""

    val s2 = """{
    "code": "SUCCESS",
    "message": "OK",
    "data": {
        "channel": "TOKOPEDIA_ID",
        "genieShopId": "SH607673E9DC0E820001825143",
        "genieMerchantId": "MC6001B465E82886000174110D",
        "genieTransactionId": "a4b15a8bffd6a5be",
        "channelShopId": "10982627",
        "channelSessionId": "1735711991",
        "messageList": [
            {
                "senderId": "185924262",
                "sendTime": "2022-02-14T06:28:28.023Z",
                "genieChatMessageSendStatus": "SUCCESS",
                "genieChatMessageType": "EMOJI",
                "genieChatSenderType": "BUYER",
                "messageContent": {
                    "content": "Yaaaaaaaaaah"
                }
            }
        ]
    },
    "pricingStrategy": "PAY"
}"""
    return arrayListOf(s.replace("\n", "").replace(" ", ""),
        s2.replace("\n", "").replace(" ", ""))
}
